package com.example.studyapp

import android.content.Intent
import android.os.Bundle
import android.widget.Button
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.android.volley.VolleyError
import com.example.studyapp.data.jsonInteraction
import com.example.studyapp.data.myRecyclerAdapter
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import org.json.JSONObject
import java.lang.Exception

//题库发送
data class json_qu_send(val dataclass: String)

//题库界面
class study_qu : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_study_qu)
        //val mytest:TextView=this.findViewById(R.id.mytest1)
        val study_qu_ret: Button = this.findViewById(R.id.study_qu_ret)
        //访问网络，获取题库列表信息
        val jsondata=json_qu_send("paper_list")
        val jsonobj=JSONObject(Gson().toJson(jsondata))
        study_qu_ret.setOnClickListener {
            this.finish()
        }
        object : jsonInteraction(jsonobj){
            override fun myResposeListener(response: JSONObject) {
                //解析列表
                init_list(response)
                //Toast.makeText(this@study_qu,init_list(response),Toast.LENGTH_LONG)
                  //  .show()
            }
            override fun myResposeErrorListener(err: VolleyError) {
                Toast.makeText(this@study_qu,err.toString(),Toast.LENGTH_SHORT)
                    .show()
            }
        }
    }

    //填充选择试卷界面
    fun init_list(response:JSONObject):String{
        val jspl:jstruct_paper_list
        try{
            jspl=Gson().fromJson(response.toString(),jstruct_paper_list::class.java)
        }catch(e:Exception){
            //return "获取题库失败"
            return e.toString()
        }
        if(jspl.error){//如果出错，说明可能
            return "题库读取失败"
        }
        if(jspl.length<=0){
            return "题库没有内容"
        }
        //初始化列表
        val layoutManager=LinearLayoutManager(this)
        val mre:RecyclerView=this.findViewById(R.id.study_qu_recyclerview)
        mre.layoutManager=layoutManager
        //将JSONArray转换为ArrayList
        var mylist=jsonToList<String>(jspl.listname.toString())
        val adapter=object : myRecyclerAdapter(mylist as ArrayList<String>){
            //设置列表点击事件
            override fun onItemClick(holder: MyViewHolder, position: Int) {
                    thisOnItemClick(holder,position)
            }
        }
        mre.adapter=adapter
        return ""
    }
    //JsonArray转换成List
    private fun <T> jsonToList(jsonList: String): List<T> {
        return Gson().fromJson(jsonList, object : TypeToken<ArrayList<T>>() {}.type)
    }
    //设置点击布局
    fun thisOnItemClick(holder: myRecyclerAdapter.MyViewHolder, position: Int){
        //设置study_sub标题和界面属性
        define_study_sub_title=holder.title.text.toString()
        define_study_sub_arg1=layout_class.LAYOUT_CLASS_QU
        //获取选中题目的信息，holder.title表示选中的试卷
        //发送消息

        //切换到做题界面
        var intent= Intent()
        intent.setClass(this , study_sub::class.java)
        this.startActivity(intent)
    }

}